package com.dtcnet.skmvp.network.simpleNet.owner;

import androidx.lifecycle.LifecycleOwner;

import com.rxjava.rxlife.RxLife;
import com.dtcnet.skmvp.network.simpleNet.ObserverUtil;
import com.dtcnet.skmvp.network.simpleNet.SkObservable;

import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class SkOwnerObservableSet<T> {
    private final Observable<T> upStream;
    private final LifecycleOwner owner;

    public SkOwnerObservableSet(Observable<T> upStream, LifecycleOwner owner) {
        this.upStream = upStream;
        this.owner = owner;
    }

    public final void subscribe(Observer<? super T> observer) {
        upStream.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .to(RxLife.to(owner))
                .subscribe(observer);
    }

    public final void subscribe(SkObservable<T> skObservable) {
        subscribe(null, skObservable);
    }

    public final void subscribeOnIo(SkObservable<T> skObservable) {
        subscribeOnIo(null, skObservable);
    }

    public final void subscribe(List<Disposable> bind, SkObservable<T> skObservable) {
        upStream.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .to(RxLife.to(owner))
                .subscribe(ObserverUtil.getObservable(bind, skObservable));
    }

    public final void subscribeOnIo(List<Disposable> bind, SkObservable<T> skObservable) {
        upStream.subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .to(RxLife.to(owner))
                .subscribe(ObserverUtil.getObservable(bind, skObservable));
    }
}
